package com.jzh.consumer.controller;

import com.jzh.constant.MessageConstant;
import com.jzh.entity.PageResult;
import com.jzh.entity.QueryPageBean;
import com.jzh.entity.Result;
import com.jzh.pojo.Permission;
import com.jzh.service.PermissionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限管理
 * @author jzh971018
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference(version = "1.0.0")
    private PermissionService permissionService;


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try{
            permissionService.add(permission);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try{
            permissionService.edit(permission);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Permission> list = permissionService.findAll();
            return  new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/removeBatch")
    public Result removeBatch(Integer[] ids){
        try {
            permissionService.removeBatch(ids);
            return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }
}
